using System;
using System.Data.SqlTypes;
using System.Globalization;

namespace Framework.Library.Core.Utils.Miscellaneous
{
    public static class DateTimeUtils
    {
        public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);

        public static DateTime ToSqlServerPrecision(DateTime value)
        {
            var sqlValue = new SqlDateTime(value);

            return sqlValue.Value;
        }

        public static DateTime ConvertUnixSeconds(long unixSeconds, long residualNanoseconds)
        {
            DateTime actualDateTime = ConvertUnixSeconds(unixSeconds);

            long ticks = residualNanoseconds/100;

            return actualDateTime.AddTicks(ticks);
        }

        public static DateTime ConvertUnixSeconds(long unixSeconds)
        {
            return UnixEpoch.AddSeconds(unixSeconds);
        }

        public static string ToFileSortableDateTime(DateTime value)
        {
            return value.ToString("yyyyMMdd'T'HHmmss", CultureInfo.CurrentCulture);
        }

        public static DateTime FromFileSortableDateTime(string value)
        {
            return DateTime.ParseExact(value, "yyyyMMdd'T'HHmmss", CultureInfo.CurrentCulture);
        }
    }
}